package UILayer;

import java.awt.BorderLayout;

import CtrLayer.StockCtr;
import ModelLayer.CurrentAccount;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class ProductManager extends JInternalFrame
{

	private JPanel contentPane;
	private JTextField name_tf;
	private JTextField art_nr_tf;
	private JTextField cat_tf;
	private JTextField quantity_tf;
	private JTextField min_am_tf;
	private JTextField piece_in_pal_tf;
	private JTextField max_am_tf;
	private JTextField group_nr_tf;
	private JTextField am_qual_for_dis_tf;
	private JTextField dis_tf;
	private JTextField price_for_pal_tf;
	private JTextField price_tf;
	private String art_nr;
	private boolean is_details;
	private JComboBox comboBox;
	private JPanel panel;
	private StockCtr stock_ctr;
	private JLabel[] error_lbls;
	private JLabel[] empty_error_lbls;
	private JLabel price_too_high_lbl;
	private JLabel piece_in_pal_too_high_lbl;
	private JLabel quant_too_high_lbl;
	private JLabel min_am_too_high_lbl;
	private JLabel wrong_dis_lbl;
	private String old_name;
	private JLabel am_for_dis_too_high_lbl;
	private boolean is_employee;
	private boolean complete;

	/**
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { UIManager.setLookAndFeel(
	 * UIManager.getSystemLookAndFeelClassName()); ProductManager frame = new
	 * ProductManager(null); frame.setVisible(true); } catch (Exception e) {
	 * e.printStackTrace(); } } }); }
	 */

	/**
	 * Create the frame.
	 */
	public ProductManager(String i_art_nr, boolean i_is_details, boolean i_complete)
	{

		super("Add new product.", true, true, true, true);
		if (i_is_details)
		{
			this.setTitle("Product details");
		}
		else if(i_complete)
		{
			this.setTitle("Complete product");
		}
		else if (i_art_nr != null)
		{
			this.setTitle("Edit product");
		}

		is_details = i_is_details;
		art_nr = i_art_nr;
		complete = i_complete;
		stock_ctr = new StockCtr();
		is_employee = CurrentAccount.give_rank().equals("employee");
		// setDefaultCloseOperation(JInternalFrame.EXIT_ON_CLOSE);

		setBounds(100, 100, 823, 456);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		if (is_details)
		{
			make_exit_btn();
		}
		else
		{
			make_ctr_btns();
		}

		make_lbls();
		make_tfs();
		make_error_lbls();
		make_empty_error_lbls();
		make_logical_error_lbls();

	}

	private void make_exit_btn()
	{
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);

		JButton exit_btn = new JButton("Ok");
		exit_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});

		panel_1.add(exit_btn);
	}

	private void make_ctr_btns()
	{
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		JButton ok_btn = new JButton("Ok");
		ok_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try_to_create_or_edit_product();
			}
		});
		panel_1.add(ok_btn, BorderLayout.WEST);

		JButton cancel_btn = new JButton("Cancel");
		cancel_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		panel_1.add(cancel_btn, BorderLayout.EAST);
	}

	private void make_logical_error_lbls()
	{
		quant_too_high_lbl = new JLabel(
				"The quantity must be lower than maximum amount.");
		quant_too_high_lbl.setForeground(Color.RED);
		quant_too_high_lbl.setLabelFor(quantity_tf);
		quant_too_high_lbl.setBounds(209, 191, 257, 14);
		quant_too_high_lbl.setVisible(false);
		panel.add(quant_too_high_lbl);

		min_am_too_high_lbl = new JLabel(
				"The minimum amount must be lower than maximum one.");
		min_am_too_high_lbl.setForeground(Color.RED);
		min_am_too_high_lbl.setBounds(209, 222, 290, 14);
		min_am_too_high_lbl.setVisible(false);
		panel.add(min_am_too_high_lbl);

		piece_in_pal_too_high_lbl = new JLabel(
				"Number of pieces in palette must be lower than maximum amount");
		piece_in_pal_too_high_lbl.setForeground(Color.RED);
		piece_in_pal_too_high_lbl.setBounds(209, 281, 321, 20);
		piece_in_pal_too_high_lbl.setVisible(false);
		panel.add(piece_in_pal_too_high_lbl);

		price_too_high_lbl = new JLabel(
				"Price per palette must be higher than price");
		price_too_high_lbl.setForeground(Color.RED);
		price_too_high_lbl.setBounds(509, 73, 206, 14);
		price_too_high_lbl.setVisible(false);
		panel.add(price_too_high_lbl);

		wrong_dis_lbl = new JLabel("Discount must be between 0 and 100.");
		wrong_dis_lbl.setForeground(Color.RED);
		wrong_dis_lbl.setBounds(530, 101, 185, 14);
		wrong_dis_lbl.setVisible(false);
		panel.add(wrong_dis_lbl);

		am_for_dis_too_high_lbl = new JLabel(
				"<html><p>Amount qualified for discount must be lower than maimum amount</p></html>");
		am_for_dis_too_high_lbl.setForeground(Color.RED);
		am_for_dis_too_high_lbl.setBounds(513, 121, 202, 45);
		am_for_dis_too_high_lbl.setVisible(false);
		panel.add(am_for_dis_too_high_lbl);
	}

	private void make_lbls()
	{
		JLabel lblNewLabel = new JLabel("Product data");
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblNewLabel.setBounds(10, 11, 138, 14);
		panel.add(lblNewLabel);

		JLabel art_nr_lbl = new JLabel("Art. number");
		art_nr_lbl.setBounds(10, 45, 67, 14);
		panel.add(art_nr_lbl);

		JLabel name_lbl = new JLabel("Name");
		name_lbl.setBounds(10, 73, 46, 14);
		panel.add(name_lbl);

		JLabel cat_lbl = new JLabel("Category");
		cat_lbl.setBounds(10, 101, 46, 14);
		panel.add(cat_lbl);

		JLabel loc_lbl = new JLabel("Location");
		loc_lbl.setBounds(10, 129, 86, 14);
		panel.add(loc_lbl);

		JLabel quantity_lbl = new JLabel("Quantity");
		quantity_lbl.setBounds(10, 191, 46, 14);
		panel.add(quantity_lbl);

		JLabel max_am_lbl = new JLabel("Maximum amount");
		max_am_lbl.setBounds(10, 253, 86, 14);
		panel.add(max_am_lbl);

		JLabel min_am_lbl = new JLabel("Minimum amount");
		min_am_lbl.setBounds(10, 222, 79, 14);
		panel.add(min_am_lbl);

		JLabel piece_in_pal_lbl = new JLabel("Pieces in a palette");
		piece_in_pal_lbl.setBounds(10, 284, 93, 14);
		panel.add(piece_in_pal_lbl);

		JLabel am_qual_for_dis_lbl = new JLabel("Amount qualified for discount");
		am_qual_for_dis_lbl.setBounds(258, 129, 145, 14);
		panel.add(am_qual_for_dis_lbl);

		JLabel group_nr_lbl = new JLabel("Group number");
		group_nr_lbl.setBounds(10, 160, 86, 14);
		panel.add(group_nr_lbl);

		JLabel price_lbl = new JLabel("Price");
		price_lbl.setBounds(258, 45, 46, 14);
		panel.add(price_lbl);

		JLabel price_for_pal_lbl = new JLabel("Price for palette");
		price_for_pal_lbl.setBounds(258, 73, 80, 14);
		panel.add(price_for_pal_lbl);

		JLabel dis_lbl = new JLabel("Discount");
		dis_lbl.setBounds(258, 101, 46, 14);
		panel.add(dis_lbl);

		JLabel percentage_lbl = new JLabel("%");
		percentage_lbl.setBounds(503, 101, 17, 14);
		panel.add(percentage_lbl);
		
		if(!(is_details) && is_employee)
		{
			price_lbl.setVisible(false);
			price_for_pal_lbl.setVisible(false);
			dis_lbl.setVisible(false);
			percentage_lbl.setVisible(false);
			am_qual_for_dis_lbl.setVisible(false);
		}
		
	}

	private void make_tfs()
	{
		name_tf = new JTextField();
		name_tf.setBounds(113, 70, 86, 20);
		panel.add(name_tf);
		name_tf.setColumns(10);

		art_nr_tf = new JTextField();
		art_nr_tf.setText(stock_ctr.get_next_art_nr());
		art_nr_tf.setEnabled(false);
		art_nr_tf.setEditable(false);
		art_nr_tf.setBounds(113, 42, 86, 20);
		panel.add(art_nr_tf);
		art_nr_tf.setColumns(10);

		cat_tf = new JTextField();
		cat_tf.setBounds(113, 98, 86, 20);
		panel.add(cat_tf);
		cat_tf.setColumns(10);

		quantity_tf = new JTextField();
		quantity_tf.setBounds(113, 188, 86, 20);
		panel.add(quantity_tf);
		quantity_tf.setColumns(10);

		min_am_tf = new JTextField();
		min_am_tf.setBounds(113, 219, 86, 20);
		panel.add(min_am_tf);
		min_am_tf.setColumns(10);

		piece_in_pal_tf = new JTextField();
		piece_in_pal_tf.setBounds(113, 281, 86, 20);
		panel.add(piece_in_pal_tf);
		piece_in_pal_tf.setColumns(10);

		max_am_tf = new JTextField();
		max_am_tf.setBounds(113, 250, 86, 20);
		panel.add(max_am_tf);
		max_am_tf.setColumns(10);

		group_nr_tf = new JTextField();
		group_nr_tf.setBounds(113, 157, 86, 20);
		group_nr_tf.setEnabled(false);
		panel.add(group_nr_tf);
		group_nr_tf.setColumns(10);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "DIY", "T",
				"KB" }));
		comboBox.setBounds(113, 126, 86, 20);
		comboBox.addItemListener(new ItemListener()
		{
			@Override
			public void itemStateChanged(ItemEvent e)
			{
				if ("KB".equals(comboBox.getSelectedItem()))
				{
					group_nr_tf.setEnabled(true);
				}
				else
				{
					group_nr_tf.setText("");
					group_nr_tf.setEnabled(false);
				}
			}
		});
		panel.add(comboBox);

		
		am_qual_for_dis_tf = new JTextField();
		am_qual_for_dis_tf.setBounds(413, 126, 86, 20);
		panel.add(am_qual_for_dis_tf);
		am_qual_for_dis_tf.setColumns(10);

		dis_tf = new JTextField();
		dis_tf.setBounds(413, 98, 86, 20);
		panel.add(dis_tf);
		dis_tf.setColumns(10);

		price_for_pal_tf = new JTextField();
		price_for_pal_tf.setBounds(413, 70, 86, 20);
		panel.add(price_for_pal_tf);
		price_for_pal_tf.setColumns(10);

		price_tf = new JTextField();
		price_tf.setBounds(413, 42, 86, 20);
		panel.add(price_tf);
		price_tf.setColumns(10);

		if (art_nr != null)
		{
			fill_tfs();
		}

		if (is_details)
		{
			just_for_view();
		}
		else
		{
			if(is_employee)
			{
				am_qual_for_dis_tf.setVisible(false);
				dis_tf.setVisible(false);
				price_for_pal_tf.setVisible(false);
				price_tf.setVisible(false);
				
			}
		}
	}

	private void just_for_view()
	{
		fill_tfs();
		name_tf.setEnabled(false);
		cat_tf.setEnabled(false);
		comboBox.setEnabled(false);
		price_tf.setEnabled(false);
		price_for_pal_tf.setEnabled(false);
		piece_in_pal_tf.setEnabled(false);
		quantity_tf.setEnabled(false);
		max_am_tf.setEnabled(false);
		min_am_tf.setEnabled(false);
		am_qual_for_dis_tf.setEnabled(false);
		group_nr_tf.setEnabled(false);
		dis_tf.setEnabled(false);
	}

	private void fill_tfs()
	{
		String[] data = stock_ctr.give_string_array(art_nr);
		old_name = data[1];

		art_nr_tf.setText(data[0]);
		name_tf.setText(data[1]);
		cat_tf.setText(data[2]);
		comboBox.setSelectedItem(data[3]);
		price_tf.setText(data[4]);
		price_for_pal_tf.setText(data[5]);
		piece_in_pal_tf.setText(data[6]);
		quantity_tf.setText(data[7]);
		max_am_tf.setText(data[8]);
		min_am_tf.setText(data[9]);
		am_qual_for_dis_tf.setText(data[10]);
		if (data[3].equals("KB"))
		{
			group_nr_tf.setText(data[11]);
			group_nr_tf.setEnabled(true);
		}
		dis_tf.setText(data[12]);
		
		if(complete)
		{
			name_tf.setEnabled(false);
			cat_tf.setEnabled(false);
			comboBox.setEnabled(false);
			piece_in_pal_tf.setEnabled(false);
			quantity_tf.setEnabled(false);
			max_am_tf.setEnabled(false);
			min_am_tf.setEnabled(false);
			group_nr_tf.setEnabled(false);
		}
	}

	private void try_to_create_or_edit_product()
	{
		if (check_for_correctness())
		{
			String name = name_tf.getText();
			String category = cat_tf.getText();
			String department = (String) comboBox.getSelectedItem();
			int quantity = Integer.parseInt(quantity_tf.getText());
			int maximum_amount = Integer.parseInt(max_am_tf.getText());
			int minimum_amount = Integer.parseInt(min_am_tf.getText());
			int pieces_in_palette = Integer.parseInt(piece_in_pal_tf.getText());
			int amount_qualified_for_discount;
			double discount;
			double price;
			double price_for_palette;
			
			if(!(is_employee))
			{
				amount_qualified_for_discount = Integer
						.parseInt(am_qual_for_dis_tf.getText());
				discount = Double.parseDouble(dis_tf.getText());
				price = Double.parseDouble(price_tf.getText());
				price_for_palette = Double.parseDouble(price_for_pal_tf
						.getText());
			}
			else
			{
				amount_qualified_for_discount = 0;
				discount = 0;
				price = 0;
				price_for_palette = 0;
			}
			String group_nr;
			if (department.equals("KB"))
			{
				group_nr = group_nr_tf.getText();
			}
			else
			{
				group_nr = "0";
			}

			if (check_for_sense(quantity, minimum_amount, maximum_amount,
					pieces_in_palette, price, price_for_palette, discount,
					amount_qualified_for_discount))
			{
				if (art_nr == null)
				{
					if (!(stock_ctr.is_such_name(name)))
					{
						stock_ctr.add_product(name, category, department,
								price, price_for_palette, pieces_in_palette,
								quantity, maximum_amount, minimum_amount,
								amount_qualified_for_discount, group_nr,
								discount);
						JOptionPane.showMessageDialog(this,
								"Successully created a product.", "Success",
								JOptionPane.INFORMATION_MESSAGE);
						this.dispose();
					}
					else
					{
						JOptionPane
								.showMessageDialog(
										this,
										"Couldn't create a product.\nProduct with this name already exists.",
										"Error",
										JOptionPane.INFORMATION_MESSAGE);
					}
				}
				else
				{
					if (!(stock_ctr.is_such_name(name))
							|| name.equals(old_name))
					{
						stock_ctr.edit_quantity(art_nr, quantity);
						stock_ctr.edit_category(art_nr, category);
						stock_ctr.edit_department(art_nr, department);
						
						stock_ctr.edit_group_nr(art_nr, group_nr);
						stock_ctr.edit_maximum_amount(art_nr, maximum_amount);
						stock_ctr.edit_minimum_amount(art_nr, minimum_amount);
						stock_ctr.edit_name(art_nr, name);
						stock_ctr.edit_pieces_in_palette(art_nr,
								pieces_in_palette);
						if(!(is_employee))
						{
							stock_ctr.edit_price(art_nr, price_for_palette);
							stock_ctr.edit_price_for_palette(art_nr,
									price_for_palette);
							stock_ctr.edit_amount_qualified_for_discount(art_nr,
									amount_qualified_for_discount);
							stock_ctr.edit_discount(art_nr, discount);
						}
						
						JOptionPane.showMessageDialog(this,
								"Successully edited a product.", "Success",
								JOptionPane.INFORMATION_MESSAGE);
						dispose();
					}
					else
					{
						JOptionPane
								.showMessageDialog(
										this,
										"Couldn't create a product.\nProduct with this name already exists.",
										"Error",
										JOptionPane.INFORMATION_MESSAGE);
					}
				}
			}
		}
	}

	private boolean check_for_sense(int quantity, int minimum_amount,
			int maximum_amount, int pieces_in_palette, double price,
			double price_for_palette, double discount,
			int amount_qualified_for_discount)
	{
		boolean returner = true;
		if (quantity > maximum_amount)
		{
			quant_too_high_lbl.setVisible(true);
			returner = false;
		}
		if (minimum_amount > maximum_amount)
		{
			min_am_too_high_lbl.setVisible(true);
			returner = false;
		}
		
		if (pieces_in_palette > maximum_amount)
		{
			piece_in_pal_too_high_lbl.setVisible(true);
			returner = false;
		}
		
		if(!(is_employee))
		{
			if (price > price_for_palette)
			{
				price_too_high_lbl.setVisible(true);
				returner = false;
			}
	
			if (amount_qualified_for_discount > maximum_amount)
			{
				am_for_dis_too_high_lbl.setVisible(true);
				returner = false;
			}
	
			if (discount > 100 || discount < 0)
			{
				wrong_dis_lbl.setVisible(true);
				returner = false;
			}
		}
		return returner;
	}

	private boolean check_for_correctness()
	{
		quant_too_high_lbl.setVisible(false);
		min_am_too_high_lbl.setVisible(false);
		piece_in_pal_too_high_lbl.setVisible(false);
		price_too_high_lbl.setVisible(false);
		empty_error_lbls[0].setVisible(false);
		empty_error_lbls[1].setVisible(false);
		am_for_dis_too_high_lbl.setVisible(false);

		boolean[] display_lbl = { true, true, true, true, true, true, true,
				true, true };
		for (int i = 0; i <= 8; i++)
		{
			error_lbls[i].setVisible(false);
		}
		if (int_convertable(quantity_tf.getText()))
		{
			display_lbl[1] = false;
		}
		if (int_convertable(min_am_tf.getText()))
		{
			display_lbl[2] = false;
		}
		if (int_convertable(max_am_tf.getText()))
		{
			display_lbl[3] = false;
		}
		if (int_convertable(piece_in_pal_tf.getText()))
		{
			display_lbl[4] = false;
		}
		if (int_convertable(group_nr_tf.getText())
				|| !(group_nr_tf.isEnabled()))
		{
			display_lbl[0] = false;
		}
		
		if(!(is_employee))
		{
			if (int_convertable(am_qual_for_dis_tf.getText()))
			{
				display_lbl[8] = false;
			}
			if (double_convertable(dis_tf.getText()))
			{
				display_lbl[7] = false;
			}
			if (double_convertable(price_tf.getText()))
			{
				if(Double.parseDouble(price_tf.getText()) > 0)
				{
					display_lbl[5] = false;
				}
			}
			if (double_convertable(price_for_pal_tf.getText()))
			{
				display_lbl[6] = false;
			}
		}
		else
		{
			display_lbl[5] = false;
			display_lbl[6] = false;
			display_lbl[7] = false;
			display_lbl[8] = false;
		}

		return display_error_lbls(display_lbl);
	}

	private boolean display_error_lbls(boolean[] display_lbl)
	{
		boolean returner = true;
		if (name_tf.getText().equals(""))
		{
			empty_error_lbls[0].setVisible(true);
			returner = false;
		}
		if (cat_tf.getText().equals(""))
		{
			empty_error_lbls[1].setVisible(true);
			returner = false;
		}

		for (int i = 0; i <= 8; i++)
		{
			if (display_lbl[i])
			{
				returner = false;
				error_lbls[i].setVisible(true);

			}
		}
		return returner;
	}

	private void make_error_lbls()
	{
		int[][] coords = { { 209, 160 }, { 209, 191 }, { 209, 222 },
				{ 209, 253 }, { 209, 284 }, { 516, 45 }, { 516, 73 },
				{ 516, 101 }, { 516, 129 } };
		error_lbls = new JLabel[9];
		for (int i = 0; i <= 8; i++)
		{
			error_lbls[i] = new JLabel("Insert number correctly.");
			error_lbls[i].setForeground(Color.RED);
			error_lbls[i].setBounds(coords[i][0], coords[i][1], 150, 14);
			error_lbls[i].setVisible(false);
			panel.add(error_lbls[i]);
		}
	}

	private void make_empty_error_lbls()
	{
		int[][] coords = { { 209, 73 }, { 209, 101 } };
		empty_error_lbls = new JLabel[3];
		for (int i = 0; i <= 1; i++)
		{
			empty_error_lbls[i] = new JLabel("Fill.");
			empty_error_lbls[i].setForeground(Color.RED);
			empty_error_lbls[i].setBounds(coords[i][0], coords[i][1], 150, 14);
			empty_error_lbls[i].setVisible(false);
			panel.add(empty_error_lbls[i]);
		}

	}

	private boolean int_convertable(String data)
	{
		try
		{
			int i = Integer.parseInt(data);
			if (i < 0)
			{
				return false;
			}
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}

	private boolean double_convertable(String data)
	{
		try
		{
			double d = Double.parseDouble(data);
			if (d < 0)
			{
				return false;
			}
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}
}
